package com.hqyj.yanguangzhi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.common.dao.ResultMapper;
import com.hqyj.common.dao.TestPaperMapper;
import com.hqyj.common.pojo.Result;
import com.hqyj.common.pojo.SysUser;
import com.hqyj.common.pojo.TestPaper;
import com.hqyj.common.util.CommonResult;
import com.hqyj.yanguangzhi.service.AMService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * YanGG
 *
 * @ClassName: AMServiceImpl
 * @author: yanguangzhi
 * @date: 2021/10/21 9:21
 */
@Service
public class AMServiceImpl implements AMService {
    @Resource
    private ResultMapper resultMapper;

    @Resource
    private TestPaperMapper testPaperMapper;

    /**
     * 同学通过session中的Id来查询自己考试的成绩
     *
     * @param userId
     * @return
     */
    @Override
    public CommonResult queryResultByUserId(Integer userId) {
        QueryWrapper<Result> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("examinee_id", userId);
        List<Result> results = resultMapper.selectList(queryWrapper);
        if (results.size() != 0) {
            return new CommonResult(200, "查询成功！", results);
        }
        return new CommonResult(400, "啥也没有呀！");
    }


    /**
     * 通过试卷名排序成绩
     *
     * @param result
     * @return
     */
    @Override
    public CommonResult queryResultByTestPaperAndNameWithDesc(Result result) {
        Page<Result> pages = new Page<Result>(result.getCurrentPage(), result.getPageSize());
        QueryWrapper<Result> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("test_paper_name", result.getTestPaperName());
        queryWrapper.orderByDesc("score");
        Page<Result> resultPage = resultMapper.selectPage(pages, queryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("results", resultPage.getRecords());
        map.put("pages", resultPage.getPages());
        map.put("currentPage", resultPage.getCurrent());
        map.put("total", resultPage.getTotal());
        map.put("pageSize", pages.getSize());
        return new CommonResult(200, "查询成功！", map);
    }

    /**
     * 通过试卷名来查看通过率和平均成绩
     *
     * @param testPaperName
     * @return
     */
    @Override
    public CommonResult queryAvgAndPassRateByTestPaperName(String testPaperName) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("avg", resultMapper.queryAvgTestPaperName(testPaperName));
        map.put("passRate", resultMapper.queryAndPassRateByTestPaperName(testPaperName));
        return new CommonResult(200, "查询成功！", map);
    }

    @Override
    public CommonResult queryResults(SysUser user) {
        QueryWrapper<Result> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("clazz", user.getClazz());
        List<Result> results = resultMapper.selectList(queryWrapper);
        return new CommonResult(200, "查询成功！", results);
    }

    /**
     * 通过学生Id来查询自己参与考试的列表
     *
     * @param userId
     * @return
     */
    @Override
    public CommonResult queryTestPaperByUserId(TestPaper testPaper, Integer userId) {
        Page<Result> resultPage = new Page<Result>(testPaper.getCurrentPage(), testPaper.getPageSize());
        QueryWrapper<Result> queryWrapper = new QueryWrapper<>();
        if (testPaper.getKeyword() != null && !testPaper.getKeyword().equals("")) {
            System.out.println("非空！！！！！！！！");
        }

        queryWrapper.like(testPaper.getKeyword() != null && !testPaper.getKeyword().equals(""),
                "test_paper_name", testPaper.getKeyword());
        queryWrapper.eq("examinee_id", userId);
        Page<Result> pages = resultMapper.queryTestPaperByExamineeId(resultPage, queryWrapper);
        ArrayList<TestPaper> testPapers = new ArrayList<>();
        for (Result record : pages.getRecords()) {
            testPapers.add(record.getTestPaper());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("testPapers", testPapers);
        map.put("pages", pages.getPages());
        map.put("total", pages.getTotal());
        map.put("pageSize", pages.getSize());
        map.put("currentPage", pages.getCurrent());
        return new CommonResult(200, "查询成功！", map);
    }

    /**
     * 老师通过班级号查询自己所带班级的试卷列表
     *
     * @param clazz
     * @return
     */
    @Override
    public CommonResult queryTestPaperByClazz(TestPaper testPaper, String clazz) {
        Page<Result> resultPage = new Page<Result>(testPaper.getCurrentPage(), testPaper.getPageSize());
        QueryWrapper<Result> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(testPaper.getKeyword() != null && !testPaper.getKeyword().equals(""),
                "test_paper_name", testPaper.getKeyword());
        queryWrapper.eq("clazz", clazz);
        Page<Result> pages = resultMapper.queryTestPaperByClazz(resultPage, queryWrapper);
        ArrayList<TestPaper> testPapers = new ArrayList<>();
        for (Result record : pages.getRecords()) {
            testPapers.add(record.getTestPaper());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("testPapers", testPapers);
        map.put("pages", pages.getPages());
        map.put("total", pages.getTotal());
        map.put("pageSize", pages.getSize());
        map.put("currentPage", pages.getCurrent());
        return new CommonResult(200, "查询成功！", map);
    }
}
